$(function() {
	new Vue({
		el: '#addHomeShopBgApp',
		data: {
			fullscreenLoading: false,
			uploadUrl: "http://39.100.131.147:8888/goods/address/uploadFile",
			form: {
				photo:""
			},
			fileAvatarList: [],
			carouseImgUrl:[],
			options:[],
			shopValue: "",
			shopSelectionTable: [],
			dialogEditFormVisible:false,
			formLabelWidth:"120px",
			editFrom:{
				name:"",
				photo:"",
				oldId:"",
			},
			editShopValue:"",
		},
		created: function() {
			parent.vueData.$data.hreHtml = "../addHomeShopBg/index.html";
			parent.vueData.activeIndex = "3";
			parent.vueData.getLeftNav("3")
			parent.vueData.$data.categoryIndex = 7;
			this.getAllCategoryShopSelection()
			this.getShopList()
		},
		methods: {
			handleImgSuccess(res, file) {
				if (res.code == 100) {
					this.form.photo = res.result
					let obj = new Object();
					obj.url = res.result;
					obj.name = "店铺精选图";
					this.fileAvatarList.push(obj)
				}
			},
			beforeImgRemove(file, fileList) {

				let index = this.fileAvatarList.findIndex(fileItem => {
					return fileItem.url === file.url
				})
				this.fileAvatarList.splice(index, 1)
				return true;
			},
			handleImgExceed(files, fileList) {
				this.$message.warning("店铺精选图只能上传一张图片")
			},
			beforeCourseRemove(file, fileList) {
				  let index = this.carouseImgUrl.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.carouseImgUrl.splice(index, 1)
				return true;
			},
			handleCoverExceed(files, fileList) {
				this.$message.warning("背景图只能上传一张图片")
			},
			handleCoverSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.editFrom.photo  = response.result
					this.carouseImgUrl.push({
						name: "店铺精选图",
			            url: response.result,
					})
					
				}
			},
			onClickAddShopSelect() {
				if (this.fileAvatarList.length <= 0) {
					this.$message.warning("请先选择店铺精选图");
					return;
				}
				if(stringEmpty(this.shopValue)){
					this.$message.warning("请先选择店铺");
					return;
				}
				let param = {
					newId:this.shopValue,
					photo:this.form.photo
				}
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.setShopSelection,
					method: 'post',
					data: param
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						 this.form = {}
						 this.fileAvatarList = []
						 this.shopValue = ""
						 this.getAllCategoryShopSelection()
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			getAllCategoryShopSelection() {
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.selectAllShopSelectionByCategory,
					method: 'post',
					data: {}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						console.log(response.data);
						this.shopSelectionTable = response.data.result
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			getShopList() {
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.selectAllShop,
					method: 'post',
					data: {}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						for (let item of response.data.result) {
							let obj = new Object();
							obj.value = item.uid;
							obj.label = item.name;
							this.options.push(obj)
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			deleteShopSelectRow(val){
			     this.fullscreenLoading = true;
			     axios({
			     	url: POSTURL.cancelShopSelection,
			     	method: 'post',
			     	data: {
						shopId:val.uid
					}
			     }).then(response => {
			     	this.fullscreenLoading = false;
			     	if (response.data.code === 100) {
			     		 this.getAllCategoryShopSelection()
			     	} else {
			     		this.$message.error(response.data.message);
			     	}
			     }).catch(error => {
			     	this.fullscreenLoading = false;
			     	console.log(error);
			     	this.$message.error('服务器出错!');
			     })    
			},
			editShopSelectRow(val){
				 this.dialogEditFormVisible = true
				  this.carouseImgUrl= [];
				  this.carouseImgUrl.push({
				  	name: "店铺精选图",
				     url: val.advertisement,
				  })
				  this.editFrom.name = val.name
				  this.editFrom.oldId = val.uid
				  this.editShopValue  = val.uid
				  this.editFrom.photo = val.advertisement
			},
			configEditUpload(){
				if (this.carouseImgUrl.length <= 0) {
					this.$message.warning("请先选择店铺精选图");
					return;
				}
				if(stringEmpty(this.editShopValue)){
					this.$message.warning("请先选择店铺");
					return;
				}
				let param = {
					newId:this.editShopValue,
					oldId:this.editFrom.oldId,
					photo:this.editFrom.photo
				}
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.setShopSelection,
					method: 'post',
					data: param
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.dialogEditFormVisible = false
						 this.editFrom = {}
						 this.carouseImgUrl = []
						 this.editShopValue = ""
						 this.getAllCategoryShopSelection()
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			}

		}
	})
})
